/**
 * 
 */
package DAO;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import ultility.ConnectDatabase;
import ultility.Procedure;
import entities.Members;

/**
 * @author NhatHung
 *
 */
public class MemberDAO {
	static Connection con = ConnectDatabase.makeConnect();
	static CallableStatement cs = null;
	static ResultSet rs = null;

	// login: fail return null - success return member
	public static Members login(String account, String password) {
		Members m = new Members();
		try {
			cs = con.prepareCall(Procedure.SQL_SP_LOGIN);
			cs.setString(1, account);
			cs.setString(2, password);
			rs = cs.executeQuery();
			if (rs.next()) {
				m = new Members(rs.getInt(1), rs.getString(2), rs.getInt(3));
			} else {
				return null;
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			return null;
		} finally {
			try {
				rs.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return m;
	}

	// registry new account: save success return true - save fail return false
	public static boolean registry(Members m) {
		try {
			cs = con.prepareCall(Procedure.SQL_SP_REGISTRY);
			cs.setString(1, m.getAccount());
			cs.setString(2, m.getPassword());
			cs.setString(3, m.getEmail());
			cs.setInt(4, m.getRoleId());
			cs.executeQuery();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// change member role: save success return true - save fail return false
	public static boolean changeRole(int memberId, int roleId) {
		try {
			cs = con.prepareCall(Procedure.SQL_SP_CHANGE_MEMBER_ROLE);
			cs.setInt(1, memberId);
			cs.setInt(2, roleId);
			cs.executeQuery();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// check account existed or not: existed return true else return false
	public static boolean checkAccount(String account) {
		try {
			cs = con.prepareCall(Procedure.SQL_SP_CHECK_EXIST_ACCOUNT);
			cs.setString(1, account);
			rs = cs.executeQuery();
			if (rs.next()) {
				return true;
			} else
				return false;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		} finally {
			try {
				rs.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// check email existed or not: existed return true else return false
	public static boolean checkEmail(String email) {
		try {
			cs = con.prepareCall(Procedure.SQL_SP_CHECK_EXIST_EMAIL);
			cs.setString(1, email);
			rs = cs.executeQuery();
			if (rs.next()) {
				return true;
			} else
				return false;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		} finally {
			try {
				rs.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// change password
	public static boolean changePassword(String password) {
		try {
			cs = con.prepareCall(Procedure.SQL_SP_CHANGE_PASSWORD);
			cs.setString(1, password);
			cs.executeQuery();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
